﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace MikroMagazaObject
{
    public class Hafiza
    {
        Dictionary<string, Object> _cache;
        private Hafiza() { _cache = new Dictionary<string, object>(); }
        private static Hafiza _instance = null;
        public static Hafiza Ctor
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Hafiza();
                }
                return _instance;
            }
        }
        public void Set(string key, object nesne)
        {
            if (_cache.ContainsKey(key))
            {
                _cache[key] = nesne;
            }
            else
            {
                _cache.Add(key, nesne);
            }
        }
        public T Get<T>(string key)
        {
            if (_cache.ContainsKey(key))
            {
                return (T)Convert.ChangeType(_cache[key], typeof(T), null);
            }
            else
            {
                return (T)Convert.ChangeType(null, typeof(T), null);
            }
        }
        public void Remove(string key)
        {
            if (_cache.ContainsKey(key))
            {
                _cache.Remove(key);
            }
        }


        public string ConnectionString
        {
            get
            {
                SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
                sqlBuilder.DataSource = Get<string>("SRV");
                sqlBuilder.InitialCatalog = Get<string>("DB");
                sqlBuilder.MultipleActiveResultSets = true;
                //if (Program.Session[Settings.AuthenticateMode].ToString() == "Sql")
                //{
                sqlBuilder.UserID = "mikromagaza";
                sqlBuilder.Password = "bilisim";
                sqlBuilder.IntegratedSecurity = false;
                //}
                //else
                //sqlBuilder.IntegratedSecurity = true;

                return sqlBuilder.ToString();
            }
        }
    }
}
